package com.ebupt.migu.music.commons.utils;

import com.ebupt.migu.globalpopup.entity.ConditionRelation;
import com.ebupt.migu.globalpopup.enums.FropertyEnum;
import com.ebupt.migu.music.commons.selfdom.AbstractFactory;
import com.ebupt.migu.music.commons.selfdom.FactoryProducer;
import com.ebupt.migu.music.commons.selfdom.demo.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 个性化条件判断
 *
 * @author wangzhu
 * @since 2021-12-31
 */
@Slf4j
public class ConditionUtil {

    private static ConditionUtil conditionUtil=new ConditionUtil();

    private ConditionUtil(){}

    public static ConditionUtil getConditionUtil() {
       return  conditionUtil;
    }

    public  boolean getCondition(Map data, List<ConditionRelation> conditions) {
        log.info("============= 个性化过滤："+conditions.get(0).getActivityId()+" ===============");
        //初始化返回值
        boolean eq = true;
        boolean ac = true;
        boolean ap = true;
        boolean pr = true;
        boolean ch = true;
        boolean lg = true;
        boolean nt = true;
        boolean vip = true;
        boolean ex = true;
        boolean exs = true;
        boolean lv = true;
        boolean dg = true;
        boolean op = true;
        //将获取到的个性化条件配置规则按大条件分组
        Map<Integer, List<ConditionRelation>> conditionsBListMap = conditions.stream().collect(Collectors.groupingBy(ConditionRelation::getPropertyCode));
        //遍历每一个大条件
        for (Map.Entry<Integer, List<ConditionRelation>> entry : conditionsBListMap.entrySet()) {
            Integer key = entry.getKey();
            List<ConditionRelation> conditionRelations = entry.getValue();
            //获取个性化条件工厂
            AbstractFactory factory = FactoryProducer.getFactory();
            switch (key) {
                case 1:
                    Equipment equipment = factory.getEquipment();
                    eq = equipment.getEquipmen(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,eq);
                    break;
                case 2:
                    Account account = factory.getAccount();
                    ac = account.getAccount(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,ac);
                    break;
                case 3:
                    App app = factory.getApp();
                    ap = app.getApp(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,ap);
                    break;
                case 4:
//                    Province province = factory.getProvince();
//                    pr = province.getProvince(data, conditionRelations);
                    pr = compareProvince(factory,data,conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,pr);
                    break;
                case 5:
                    Channel channel = factory.getChannel();
                    ch = channel.getChannel(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,ch);
                    break;
                case 6:
                    Login login = factory.getLogin();
                    lg = login.getLogin(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,lg);
                    break;
                case 7:
                    Network network = factory.getNetwork();
                    nt = network.getNetwork(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,nt);
                    break;
                case 8:
                    Member member = factory.getMember();
                    vip = member.getMember(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,vip);
                    break;
                case 9:
                    ExType expert = factory.getExType();
                    ex = expert.getExType(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,ex);
                    break;
                case 10:
                    Growth growth = factory.getGrowth();
                    lv = growth.getGrowth(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,lv);
                    break;
                case 11:
                    ExSubType expertsub = factory.getExSubType();
                    exs = expertsub.getExSubType(data, conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,exs);
                    break;
                case 12:
                    DynamicGroup dynamicGroup = factory.getDynamicGroup();
                    dg = dynamicGroup.getDynamicGroup(data, conditionRelations.get(0));
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key), conditionRelations.get(0), dg);
                    break;
                case 13:
                    op = compareProvince(factory,data,conditionRelations);
                    log.info("前置条件类型:{},条件配置:{},对比结果:{}", FropertyEnum.getCodeByName(key),conditionRelations,pr);
                    break;
                default:
                    break;
            }

        }
        //对前置条件之间的关系作对比（第一版本只支持 and->&&）
        if (eq && ac && ap && pr && ch && lg && nt && vip && ex && lv && exs && dg && op) {
            log.info("参数:{},最终结果:{}",data,true);
            return true;
        }
        log.info("参数:{},最终结果:{}",data,false);
        return false;
    }

    private boolean compareProvince(AbstractFactory factory,Map data, List<ConditionRelation> conditionRelations){
        Province provinces = factory.getProvince();
        return provinces.getProvince(data, conditionRelations);
    }

}

